<?php

error_reporting(1);
$link = null;
$directory = "administracion/vista/";

/////CONEXION BD
if($link = mysql_connect("localhost", "root", "")) {
    if (!mysql_select_db("g2u", $link)){
        die ("la base de datos no existe.");
    }
} else {
   die ("error al conectar al motor."); 
}


/////OBTENEMOS LAS TABLAS DESDE BD
$rs = null;
$fila = null;
$rs = mysql_query("SHOW TABLES;", $link);
$tables = array();
if (is_resource($rs)){
    
    while($fila = mysql_fetch_array($rs)){
        $tables[]= $fila[0];
    }
    
} else {
    die ("LA BD NO TIENE TABLAS");
}



foreach ($tables as $key => $value) {
    $rs = null;
    $fila = null;
    $deleteByUpdate = false;
    $fieldStatus = null;
    $stringCampos = "";
    $fieldDesc = "";
    $statusIndex = "";
    
    $rs = mysql_query("DESC $value;", $link);
    $campos = array();

    while($fila = mysql_fetch_array($rs)){
        $campos[]= $fila[0];
        $stringCampos .= " $fila[0],"; 
        
        if(strpos($fila[0], "status")){
            $deleteByUpdate = true;
            $fieldStatus = $fila[0];
            $statusIndex = count($campos)-1;
        }
        
        if(strpos($fila[0], "desc")){
            $fieldDesc = $fila[0];
        }
    }
    
    $stringCampos = substr($stringCampos, 0, strlen($stringCampos)-1);
    $value2 = str_replace("_", "", $value);
    $value2 = strtoupper(substr($value2, 0, 1)).substr($value2, 1, strlen($value2));
/////CREANDO ARCHIVOS DE CLASE
    $file = $directory."vista.$value.php";
    $fp = fopen($file, 'w');
    echo date('Y-m-d H:i:s')."  ::  Create File:".$file;

    fwrite($fp, 
'<?php

');

    fwrite($fp, 
'

function form_'.$value.'($area = false){   
 
    
    if($area){

        $action = "index2.php?seccion='.$value.'&op=update";
        $btnSubmit = $_SESSION[\''.$value.'\'][\'mensaje\'][\'update_text\'];
    }else{
        $action = "index2.php?seccion='.$value.'&op=save";
        $btnSubmit = $_SESSION[\''.$value.'\'][\'mensaje\'][\'save_text\'];
    }

?>
     <script>
          $("#formulario").ready(function(){                                                 
            $("#formulario").validate({
                rules:{
                    nombre: {
                        required: true
                    }
                },
                messages:{
                    nombre:{
                        required: "(*) INGRESAR!"
                    }
                },
                submitHandler: function(form) {
                    enviarFormulario(\'<?=$action?>\',\'at\');
                }
            });      
      });
    </script>
        
        <form method="POST"  name="formulario" id="formulario" >
    <?
    if($area){
        ?>
    <input type="hidden" name="id" value="<?=$id?>" />
        <?
    }
    ?>
    <table id="tableForm'.$value2.'">
        <tr>
            <td colspan="2">&nbsp;</td>
        </tr>
');
foreach ($campos as $k => $v) {    
    fwrite($fp, 
'        <tr class="tb">
            <td class="tb11">'.$v.'</td>
            <td class="tb11"><input type="text" name="'.$v.'" id="'.$v.'" value="<?=$'.$v.'?>"  style=" width: 240px;" /></td>
        </tr>
');
}

    fwrite($fp, 
'        <tr class="tb">
            <td  class="tb11"colspan="2">
				<center>
					<input type="submit" value="<?=$btnSubmit?>"/>
				</center>
			</td>
        </tr>
        <tr>
            <td colspan="2" class="tb11"><center>-------------</center></td>
        </tr>
    </table>
</form>
<?
}  
    
');
    
    fwrite($fp, 
'    

function list_'.$value.'($data, $msj=false, $dataTotal=false, $arrCb= false){
   
    if(!is_null($data)){
        $start = 1;
        if(isset ($_REQUEST["page"])){
            $start = $_REQUEST["page"];
        }
    $pages = ceil($dataTotal / 10);
      
    ?>
       <script type="text/javascript">
            $(function() {
            $("#paginador").paginate({
                count: <?=$pages?>,
                start: <?=$start?>,
                display: 3,
                border: true,
                border_color: \'#5386a3\',
                text_color: \'#fff\',
                background_color: \'#5386a3\',	
                border_hover_color: \'#fa5909\',
                text_hover_color: \'#fff\',
                background_hover_color	: \'#fa5909\', 
                images: true,
                mouse: \'press\',
                onChange: function(page){                    
                    return enviarFormulario(\'index2.php?seccion='.$value.'&op=list&page=\'+page,\'attriaje\');
                }
            });
        });

        </script>
       
<?
    }
?>
    <script type="text/javascript">

    $(document).ready(function(){
        $("#TablaPaginador td").css("border-style", "none");
        $("#TablaPaginador td").css("border-color", "#eaeef2");        
        $("#TablaPaginador td").css("vertical-align", "middle"); 
        
        //Filtros busqueda
        $("#TablaFiltros td").css("border-style", "none");
        $("#TablaFiltros td").css("border-color", "#eaeef2");        
        $("#TablaFiltros td").css("vertical-align", "middle"); 
        $("#TablaFiltros td").css("padding", "4px"); 
                
        $("#estado").selectOptions(\'<?=$_REQUEST[\'estado\']?>\', true);
        
        $("#sede option").eq(\'\').text("Todas");
                
        $("#filtro").css("width","200px");
        $("#estado").css("width","200px");
    });
    </script>
    <br/>
<form method="POST" onsubmit="return enviarFormulario(\'index2.php?seccion='.$value.'&op=list\',\'attriaje\');" name="formulario" id="formulario">
    <fieldset style="width: 50%;">
            <legend><b><?=$_SESSION[\''.$value.'\'][\'mensaje\'][\'filtro_head\']?></legend>        
                <table id="TablaFiltros">                    
                    <tr>
                        <td> <b><?=$_SESSION[\''.$value.'\'][\'mensaje\'][\'filtro_desc\']?> :<b/> </td>
                        <td> <input type="text" id="filtro" name="filtro" value="<?=$_REQUEST["filtro"]?>" /> </td>                
                    </tr>
                    <tr>
                        <td> <b><?=$_SESSION[\''.$value.'\'][\'mensaje\'][\'filtro_estado\']?> :</b> </td>
                        <td> <select id="estado" name="estado" >
                                <option value="99"><?=$_SESSION[\''.$value.'\'][\'mensaje\'][\'filtro_estado_all\']?></option>
                        <?
                            foreach ($arrCb[\'estado\'] as $key => $value) {
                                ?>
                                <option value="<?=$key?>"><?=$value?></option>
                                <?
                            }
                        ?>
                            </select>
                        </td>                
                    </tr>
                    <tr>
                        <td colspan="2"></td>
                    </tr>
                    <tr>
                        <td colspan="2"> <input type="submit" value="<?=$_SESSION[\''.$value.'\'][\'mensaje\'][\'search\']?>"  /> </td>
                    </tr>
                </table>
            <br/>
        </fieldset>
</form> 
        <br/>
        
        <table width="100%" id="TablaPaginador" >
            <tr>
                <td width="35%">
                    <font color="blue">
                    <b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                        <?=$_SESSION[\''.$value.'\'][\'mensaje\'][\'total\']?>: <?=$dataTotal?> <?=$_SESSION[\''.$value.'\'][\'mensaje\'][\'texto\']?>
                    </b>
                   </font>
                </td>
                <td>
                     <div id="paginador"> </div>                    
                </td>
            </tr>
        </table>  
    <?
    if($msj){
        ?>
        <b><?=$msj?></b>
        <script type="text/javascript">
            $("#alertzone").fadeOut(10000);                                    
        </script>
        <?
    }
    if(!is_null($data)){
        include_once \'recursos/TablaDatos.php\';
        $TABLA = new TablaDatos();
        $cabecera = array(\'#\',\'NOMBRE\',\'ESTADO\', \'<center>EDITAR</center>\', \'<center>ACT. / NO ACT.</center>\');
        $TABLA->setCabeceraTabla($cabecera);
        $TABLA->setDataArray($data);
        $TABLA->crearTabla();             
    }
}
');
    

/////CERRAR CLASE
fwrite($fp, 
"
");         
     
    fclose($fp);
    echo " <br/>";
    echo date('Y-m-d H:i:s')."  ::  End creation File:".$file;
    echo " <br/><br/>";    
}